package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.*;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author
 * @since 2022-11-24
 */
@Mapper
public interface ActivityMemberDao extends BaseMapper<ActivityMember> {

    /**
     * 根据条件查询签到并获取成员姓名和id
     * @param page
     * @param qw
     * @return
     */
    @Select("select m.* from user u INNER JOIN activity_member m " +
            "on u.uid=m.uid " +
            "${ew.customSqlSegment}")
    @Results({
            // 被用来进行多表查询的字段uid不再返回，需要重新在结果集中声明
            @Result(property = "uid", column = "uid"),
            @Result(
                    //此处uid不需要写为m.uid和u.uid是因为多表查询是在@Select查询完之后在实体类基础上进行的
                    property = "userName", column = "uid",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.UserDao.findNameByUid")
            )
    })
    IPage<ActivityMemberDTO> getMemberState(Page<ActivityMemberDTO> page,
                                            @Param(Constants.WRAPPER) QueryWrapper<ActivityMemberDTO> qw);

    @Select("select MIN(stage) from activity_member where activity_id=#{activityId} and sign!='签到成功'")
    Integer getLeastSuccessfulStage(Long activityId);

    //@Select("select distinct stage, count from activity_member where activity_id=#{activityId} ORDER BY stage ASC, count ASC")
    @Select("select *, count(distinct stage, count) from activity_member ${ew.customSqlSegment}" +
            "GROUP BY stage, count ORDER BY stage ASC, count ASC")
    IPage<ActivityMember> getSigns(Page<ActivityMember> page,
                                   @Param(Constants.WRAPPER) QueryWrapper<ActivityMember> qw);

    @Select("select * from activity_member where activity_id = #{activityId}}")
    List<ActivityMember> getByActivityId(long activityId);
}
